-- hurricane
-- created by shuzy
-- 每回合概率触发技能（飓风）

return {
    apply = function(prop, baseValue)
        return prop[3];
    end,
    -- 触发
    trigger = function(who, prop, arg)
        if who.type ~= OBJECT_TYPE_USER then
            return;
        end

        -- 不满足概率
        if DungeonM.getRandSeed("hurricane") % 1000 >= prop[3] then
            return;
        end

        local skillId = tonumber(prop[2]);
        local skills = SkillM.getSkillTable();
        if not skills[skillId] then
            return;
        end

        -- 目标
        local grid;
        local round = CombatM.getRound();
        if not SkillM.needTarget(skillId) then
            SkillM.apply(who, who, skillId, round);
        else
            -- 遍历所有的怪
            local ret = {};

            -- 取出场上已翻开且未死亡的怪物
            for p = 1, DUNGEON_WIDTH * DUNGEON_HEIGHT do
                grid = DungeonM.getGridByPos(p);

                -- 是未死亡的怪物
                if  grid:isOpened() and
                    grid:isMonster() and
                    not grid.monster:isDead() then
                    table.insert(ret, grid.monster);
                end
            end

            local num = #ret;
            if num == 0 then
                return;
            end
            -- 随机选一个进行打击
            local rand = DungeonM.getRandSeed("hurricane") % num;
            local monster = ret[rand + 1];
            SkillM.apply(who, monster, skillId, round);       
        end
    end,

    record = function(target, prop, path)
        return false;
    end
};
